<?php


namespace App\Lib\Process;


use EasySwoole\Core\Component\Di;
use EasySwoole\Core\Component\Logger;
use EasySwoole\Core\Swoole\Process\AbstractProcess;
use Swoole\Process;

class Consumer1 extends AbstractProcess
{

    private $isRun = false;

    /**
     * 消费进程中的数据
     * 每隔500ms定时检测有没有任务, 有的话就while循环执行
     * @param Process $process
     */
    public function run(Process $process)
    {
         $this->addTick(500, function (){
            if (!$this->isRun){
                $this->isRun = true;
                while (true){
                    try {
                        $task = Di::getInstance()->get('REDIS')->lPop('list_test');
                        if ($task){
                            Logger::getInstance()->log($this->getProcessName().'____'.$task);
                        }else{
                            break;
                        }
                    }catch (\Throwable $throwable){
                        Logger::getInstance()->log("异常: ".$throwable->getMessage());
                        break;
                    }
                }
                $this->isRun = false;
            }
         });
    }

    public function onShutDown()
    {
        // TODO: Implement onShutDown() method.
    }

    public function onReceive(string $str, ...$args)
    {
        // TODO: Implement onReceive() method.
    }
}